from keystoneauth1.identity import v3
from keystoneauth1 import session
from novaclient import client


# auth_url为keystone的endpoint入口
def get_keystone_session():
    auth = v3.Password(auth_url="http://192.168.10.10:5000/v3",
                       username="admin", password="000000",
                       project_name="admin", user_domain_name="demo",
                       project_domain_name="demo")
    sess = session.Session(auth=auth)
    return sess


# 调用nova客户端
def get_nova_client():
    sess = get_keystone_session()
    nova = client.Client(2, session=sess)
    return nova


# 查询server信息
def show_server_info(nova):
    instances = nova.servers.list()
    for instance in instances:
        print(instance)


# 查询flavor信息
def show_flavor_info(nova):
    flavors = nova.flavors.list()
    for flavor in flavors:
        print(flavor)


# 程序入口
if __name__ == '__main__':
    nova = get_nova_client()
    show_server_info(nova)
    show_flavor_info(nova)
